package com.dengdai.apputils.activitys;

import android.widget.TextView;

import com.dengdai.applibrary.activitys.BaseActivity;
import com.dengdai.applibrary.https.HttpHelper;
import com.dengdai.applibrary.utils.LogUtils;
import com.dengdai.apputils.R;
import com.dengdai.apputils.service.UserEntity;
import com.dengdai.apputils.service.UserServiceImpl;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * @说明
 * @作者 LY
 * @时间 16/5/26 17:17
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public class TestRxJavaActivity extends BaseActivity {
    @Bind(R.id.btn1)
    public TextView btn1;


    @Override
    public int getLayoutResID() {
        return R.layout.activity_test_rxjava;
    }

    @Override
    public void initData() {

    }

    @Override
    public void business() {
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onCompleted() {
                LogUtils.i("=====>> onCompleted ");
            }

            @Override
            public void onError(Throwable e) {
                LogUtils.i("=====>> onError " + e.getMessage());
            }

            @Override
            public void onNext(String s) {
                LogUtils.i("=====>> onNext " + s);
            }
        };
        {
            Observable observable = Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    subscriber.onNext("========>>>>11111");
                    subscriber.onNext("========>>>>22222");
                    subscriber.onNext("========>>>>33333");
                    subscriber.onError(new Throwable("我是传说"));
                    subscriber.onCompleted();
                }
            });
            observable.subscribe(observer);
        }
        Observable observable = Observable.just("========>>>>11111", "========>>>>11111", "========>>>>11111", "========>>>>11111");
        observable.subscribe(observer);

        RxView.clicks(btn1).throttleFirst(3000, TimeUnit.MILLISECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                LogUtils.i("========>>>>>");
            }
        });
        {
            startLoadingDialoge();
            Observable.just("http://121.40.17.140:9001/v.1.2.0/api/Account/Report/SearchReport/doctorguid=35eb5adc-177f-480e-b01a-e5e9a9c9a185")
                    .map(new Func1<String, String>() {
                        @Override
                        public String call(String s) {

                            String data = null;
                            try {
                                data = HttpHelper.getInstance().sendGet(s, null, null);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return data;
                        }
                    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
                @Override
                public void call(String str) {
                    LogUtils.i("=====>>>" + str);
                    stopLoadingDialoge();
                }
            });

        }

        {//网络加载
            UserServiceImpl.getUser()
                    .searchReport("b3b467db-7c50-461d-8cb3-bb8ba25db76a")
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<UserEntity>() {
                        @Override
                        public void onCompleted() {
                            LogUtils.i("=====>>>>onCompleted ");
                        }

                        @Override
                        public void onError(Throwable e) {
                            LogUtils.i("=====>>>>onError " + e.toString());
                        }

                        @Override
                        public void onNext(UserEntity s) {

                        }
                    });
        }


    }

}
